<?php
if(!defined('ADMIN')){
	exit('Access Denied');
}
if(isset($_GET["tablename"])){
    if($_GET["tablename"]==""){
        echo "false";
        exit;
    }
    $id=intval($_GET["id"]);
    $modelid=intval($_GET["modelid"]);
    //if(empty($modelid)){
    //    echo "false";
    //    exit;        
    //}
    $field=trim($_GET["tablename"]);
    if($id>0){
        $row=$ezhand->select(MOD,'*','tablename="'.$field.'" and id<>"'.$id.'"');
    }else{
        $row=$ezhand->select(MOD,'*','tablename="'.$field.'"');
    
    }
    if(empty($row)){
        echo "true";
        exit;        
    }else{
        echo "false";
        exit;        
    }
    
}

if($_POST['sub']!=''){
    $id=empty($_POST['id'])?0:(int)$_POST['id'];
	unset($_POST['id']);
	unset($_POST['sub']);
	$data=array('title'=>trim($_POST['title']),'tablename'=>trim($_POST["tablename"]),'content'=>trim($_POST["content"]));
    if(trim($_POST['title'])==""){
        jumpajax('300','模型名称不能为空!','');
    }
    if(trim($_POST['tablename'])==""){
        jumpajax('300','数据表名不能为空!','');
    }
    $tablename=trim($_POST["tablename"]);
 	if($id==0){
        $row=$ezhand->select(MOD,'*','tablename="'.$tablename.'"');
        if(!empty($row)){
            jumpajax('300','数据表名不能重复!','');
        }
	    
        if($ezhand->creat_table($tablename)==false){
              jumpajax('300','模型创建失败','','');  
        }
        $id=$ezhand->insert(MOD,$data);
        if($id){
            $systemfield=array(
            "modelid"=>$id,"field"=>"keywords","name"=>"关键字","inputsize"=>"100","formtype"=>"text","isrequired"=>"0","issys"=>"1"
            );
            $fid=$ezhand->insert("modelfield",$systemfield);

            $systemfield=array(
            "modelid"=>$id,"field"=>"thumb","name"=>"缩略图","inputsize"=>"100","formtype"=>"image","isrequired"=>"0","issys"=>"1"
            );
            $fid=$ezhand->insert("modelfield",$systemfield);

            $systemfield=array(
            "modelid"=>$id,"field"=>"description","name"=>"简介","inputsize"=>"100","formtype"=>"area","isrequired"=>"0","issys"=>"1"
            );
            $fid=$ezhand->insert("modelfield",$systemfield);
            
            
            $systemfield=array(
            "modelid"=>$id,"field"=>"title","name"=>"标题","inputsize"=>"100","formtype"=>"text","isrequired"=>"1","issys"=>"1"
            );
            $fid=$ezhand->insert("modelfield",$systemfield);
            $systemfield=array(
            "modelid"=>$id,"field"=>"url","name"=>"链接地址","inputsize"=>"100","formtype"=>"text","isrequired"=>"1","issys"=>"1"
            );
            $fid=$ezhand->insert("modelfield",$systemfield);                        
            
            $systemfield=array(
            "modelid"=>$id,"field"=>"listorder","name"=>"排序","inputsize"=>"100","formtype"=>"int","isrequired"=>"0","issys"=>"1","tips"=>"越大越靠前","defaultval"=>0
            );
            $fid=$ezhand->insert("modelfield",$systemfield);                        

            
            
        }
                
        
        $ezhand->set_mode_cache(MOD,"id,title,tablename");
        
		jumpajax('200','保存成功','','closeCurrent');
	}
	else{
        $row=$ezhand->select(MOD,'*','tablename="'.$tablename.'" and id<>'.$id);
        if(!empty($row)){
            jumpajax('300','数据表名不能重复!','');
        }
        $row=$ezhand->select(MOD,'*','id='.$id);
        $oldtablename=$row["tablename"];
        $ezhand->rename_table($oldtablename,$tablename);	   
	    $ezhand->update(MOD,$data,'id="'.$id.'"');
        $ezhand->set_mode_cache(MOD,"id,title,tablename");
		jumpajax('200','修改成功','','closeCurrent');
	}
}
else{
	$id=empty($_GET['id'])?0:(int)$_GET['id'];
	
    if($id==0){
	    $row=array();
	}
	else{
	    $row=$ezhand->select(MOD,'*','id="'.$id.'"');
	}
}
?>